package cn.doitedu.utils;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.flink.table.functions.ScalarFunction;

import java.text.ParseException;
import java.util.Date;

public class TimeTruncUDF extends ScalarFunction {

    public String eval(String originTime, int interval){
        String truncated = null;
        try {
            Date date = DateUtils.parseDate(originTime, "yyyy-MM-dd HH:mm:ss");
            long time = date.getTime();

            //time = time - time%(interval*60*1000L);
            time  = (time / (interval*60*1000L))*(interval*60*1000L);

            truncated = DateFormatUtils.format(time, "yyyy-MM-dd HH:mm:ss");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }


        return truncated;
    }


}
